import { MockMethod } from 'vite-plugin-mock'
import { RespUtil } from './MockUtil'
import { IncomingMessage, ServerResponse } from 'http'

const buildToken = (username: string) => ({ token: username })
const buildJsonPromise = (req: IncomingMessage, res: ServerResponse, respJson: unknown, httpStatus = 200) => {
  new Promise((resolve) => {
    let reqbody = ''
    req.on('data', (chunk) => {
      reqbody += chunk
    })
    req.on('end', () => resolve(undefined))
  })
  res.setHeader('Content-Type', 'application/json')
  res.statusCode = httpStatus
  res.end(JSON.stringify(respJson))
}
export default [
  {
    url: '/api/login',
    method: 'post',
    response: ({ body }: any) => {
      switch (body.username) {
        case 'test': {
          return RespUtil.suc(buildToken(body.username))
        }

        case 'admin': {
          return RespUtil.suc(buildToken(body.username))
        }

        case 'superAdmin': {
          return RespUtil.suc(buildToken(body.username))
        }

        default: {
          return {
            token: RespUtil.exp(400, '用户名或密码错误!')
          }
        }
      }
    }
  },
  {
    url: '/api/login_user_info',
    method: 'post',
    rawResponse: async (req, res) => {
      const token = req.headers.token
      const menus = [
        {
          name: '首页',
          code: 'HomePage',
          icon: 'home',
          url: '/index/home'
        },
        {
          name: '表单',
          code: 'Form',
          icon: 'form',
          children: [
            {
              name: 'El Form',
              code: 'ElFormPage',
              url: '/index/form/el-form'
            },
            {
              name: 'JSON Form',
              code: 'JsonFormPage',
              url: '/index/form/json-form?age=12&name=张三'
            },
            {
              name: '模式切换',
              code: 'SchemaChange',
              url: '/index/form/schema-change'
            }
          ]
        },
        {
          name: '表格',
          code: 'Table',
          icon: 'table',
          children: [
            {
              name: 'JsonTable',
              code: 'JsonTable',
              url: '/index/table/json-table'
            },
            {
              name: '分页表格',
              code: 'PageTable',
              url: '/index/table/page-table'
            }
          ]
        },
        {
          name: '图表',
          code: 'Chart',
          icon: 'chart'
        },
        {
          name: '权限界面',
          code: 'Permission',
          icon: 'permission'
        },
        {
          name: '性能优化',
          code: 'OptimizeDemo',
          icon: 'fast'
        },
        {
          name: '文本编辑器',
          code: 'TextEditor',
          icon: 'text-editor'
        },
        {
          name: '多级菜单多级菜单多级菜单多级菜单多级菜单',
          code: 'LevelMenu',
          icon: 'level',
          children: [
            {
              name: '二级菜单二级菜单二级菜单二级菜单二级菜单二级菜单',
              code: 'LevelMenuTwo'
            },
            {
              name: '二级菜单-1',
              code: 'LevelMenuTwo1',
              children: [
                {
                  name: '三级菜单-1',
                  code: 'LevelMenuThree1'
                },
                {
                  name: '三级菜单-2',
                  code: 'LevelMenuThree2'
                }
              ]
            },
            {
              name: '二级菜单-2',
              code: 'LevelMenuTwo2'
            }
          ]
        }
      ]
      const simpleUserInfo = {
        name: '王图思睿56',
        nikeName: '豌豆射手',
        avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
      }
      const respJson = RespUtil.suc({ menus, simpleUserInfo })
      await buildJsonPromise(req, res,respJson)
    }
  }
] as MockMethod[]
